﻿/// <reference path="../../js/viewmodels.js" />
(function () {

    var createQueryString = function () {
        return new WinJS.Promise(function (complete) {
            //we get the elements and turn them into a winControl, then use their selection property
            var selectedCities = document.getElementById("cities").winControl;
            selectedCities.layout = new WinJS.UI.ListLayout(); //makes the grid slide verically

            var selectedSectors = document.getElementById("sectors").winControl;
            selectedCities.layout = new WinJS.UI.ListLayout(); //makes the grid slide verically
            var extensionToQuery = "";
            var selectedEmploymentOptions = document.getElementById("employmentOptions").winControl;

            //get the selected cities 
            selectedCities.selection.getItems().then(function (items) {
                if (items.length != 0) {
                    extensionToQuery = "&region=";
                    for (var i = 0; i < items.length; i++) {
                        extensionToQuery += items[i].data.id;
                        if (i != items.length - 1) {
                            extensionToQuery += ',';
                        }
                    }
                }

                //get the selected sectors 
                selectedSectors.selection.getItems().then(function (items) {
                    if (items.length != 0) {
                        extensionToQuery += "&sector=";
                        for (var i = 0; i < items.length; i++) {
                            extensionToQuery += items[i].data.id;
                            if (i != items.length - 1) {
                                extensionToQuery += ',';
                            }
                        }
                    }
                });

                //get the selected employment options
                selectedEmploymentOptions.selection.getItems().then(function (items) {
                    if (items.length != 0) {
                        extensionToQuery += "&worktime=";
                        for (var i = 0; i < items.length; i++) {
                            extensionToQuery += items[i].data.id;
                            if (i != items.length - 1) {
                                extensionToQuery += ',';
                            }
                        }
                    }
                });

                //Clear Previois List View Entries
                var listViewElement = document.getElementById("jobOffers");
                if (listViewElement != null) {
                    listViewElement.innerHTML = "";
                }
                extensionToQuery += "&_pagerRows=10000";
                //the promise is completed when the query is created
                complete(extensionToQuery);
                return extensionToQuery;
            }, function (error) {
                var errDiv = document.createElement("div");
                errDiv.id = "errorContainer";
                document.body.appendChild(errDiv);

                if (e.message != undefined) {
                    console.log(e.message);
                    errDiv.innerText = "Internal error has occured. Please try again later!";
                }
                else {
                    errDiv.innerText = "Internal error has occured. Please try again later!";
                }
            });
        });
    }

    var goToSearchResults = function (invokeEvent) {
        createQueryString().then(function (query) {
            ResultCodeBehind.callLoadJobOffers(query);
        });
        WinJS.Navigation.navigate("/pages/searchresults/searchresults.html");
    }

    var showListViewInvokedItem = function (event) {

        event.detail.itemPromise.then(function (item) {
            ResultCodeBehind.callLoadJobOffers(item.data.searchString);
            WinJS.Navigation.navigate("/pages/searchresults/searchresults.html");
        }, function (error) {
            var errDiv = document.createElement("div");
            errDiv.id = "errorContainer";
            document.body.appendChild(errDiv);

            if (e.message != undefined) {
                console.log(e.message);
                errDiv.innerText = "An error has occured while getting your favourite searches. Please try again later!";
            }
            else {
                errDiv.innerText = "An error has occured while getting your favourite searches. Please try again later!";
            }
        });
    }

    WinJS.Utilities.markSupportedForProcessing(showListViewInvokedItem);

    WinJS.Namespace.define("HomeCodeBehind", {
        callLoadCities: function () {
            ViewModels.loadCities();
        },
        callLoadSectors: function () {
            ViewModels.loadSectors();
        },
        callEmploymentTypes: function () {
            ViewModels.loadEmploymentTypes();
        },
        goToSearchResults: goToSearchResults,
        createQueryString: createQueryString,
        showListViewInvokedItem: showListViewInvokedItem
    });
})();